package com.android.people;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

import com.android.people.CoverFlowView.EndFlingListener;
import com.android.people.CoverFlowView.MidAreaClickListener;
import com.android.people.SlideImageViewService.BitmapAdapter;

public class GallaryActivity extends Activity implements EndFlingListener, MidAreaClickListener{
    private static final String TAG = "GallaryActivity";
    private static final boolean DEBUG = true;
    
    private ArrayList<Bitmap> mResourceList;
    private CoverFlowView mCFView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LOGD("create GallaryActivity");
        this.setContentView(R.layout.coverflow_gallary);
        mCFView = (CoverFlowView) this.findViewById(R.id.coverflow);
        mCFView.initAttribute(R.xml.coverflow);
        mCFView.setFlingInterface(this);
        mCFView.setMidAreaClickListener(this);

        mResourceList = new ArrayList<Bitmap>();
//        mCFView.SetFling(true);
    }
    
    @Override
    public void onStart() {
        super.onStart();
        LOGD("create GallaryActivity");
        String scanPath = getIntent().getStringExtra(SlideImageViewService.GALLARY_PATH);
        String filepath = getIntent().getStringExtra(SlideImageViewService.GALLARY_SHOW_NAME);
        if (scanPath == null) {
            scanPath = "/sdcard/preview/";
        }
        LOGD("The gallary pciture scanPath = " + scanPath);
        
        File file = new File(scanPath);
        if (file.exists() == true) {
            ImageListManager listManager = new ImageListManager(this, scanPath);
            List<Long> mIdList = listManager.getIDList();
            if (mIdList != null) {
                mResourceList = new ArrayList<Bitmap>();
                for (Long id : mIdList) {
                    Bitmap bm = listManager.thumbBitmap(id);
                    if (bm != null) {
                        mResourceList.add(bm);
                    }
                }
                int index = 0;
                mCFView.setAdapter(new BitmapAdapter(mResourceList, index));
                for (Bitmap bm : mResourceList) {
                    if (bm.isRecycled() == false) {
                        bm.recycle();
                    }
                }
                mResourceList.clear();
            }
        }
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mCFView.GCResource();
    }
    
    public void HandleEndFlingEvent(boolean isLeft) {
        mCFView.SetFling(false);
    }

    public void HandleTouchMidCoverEvent() {
        
    }
    
    public void onClick() {
    	
    }
    
    public static void LOGD(String msg) {
        if (DEBUG == true) {
            Log.d(TAG, msg);
        }
    }
}
